package com.test.it.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.it.ruiji.common.R;
import com.test.it.ruiji.entity.Employee;
import com.test.it.ruiji.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

import static com.test.it.ruiji.common.Utils.isNotEmpty;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController{

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        password  = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        if(emp==null){
            return R.error("登录失败");
        }
        if(!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }
        if(emp.getStatus() == 0){
            return R.error("账号被禁用");
        }

        System.out.println(emp.getUsername());
        request.getSession().setAttribute("employee",emp.getId());


        return R.success(emp);

    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        log.info("退出成功！");
        return R.success("退出成功");

    }

    /**
     * 新增员工
     * @param employee 员工信息
     * @return R<String>
     */
    @PostMapping
    public  R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新员工：{}",employee.toString());
//        s设置初始密码

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        String employee1 = (String) request.getSession().getAttribute("employee");
//        employee.setCreateUser(employee1);
//        employee.setUpdateUser(employee1);
        employeeService.save(employee);
        return  R.success("新增员工成功");
    }
    @GetMapping("/page")
    public R<Page<Employee>> page(int page, int pageSize, String name){
        log.info("page={},pagesize={},name={}",page,pageSize,name);
        Page<Employee> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(isNotEmpty(name),
                Employee::getName,
                name);
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(page1,lambdaQueryWrapper);

        return  R.success(page1);
    }
    @GetMapping("/{id}")
    public  R<Employee> getUser(@PathVariable("id")   long id){
        log.info(String.valueOf(id));
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.eq(Employee::getId,id);
        Employee one = employeeService.getOne(employeeLambdaQueryWrapper);
        return  R.success(one);
    }

    @PutMapping
    public R<String> updateStatus(HttpServletRequest request,@RequestBody Employee employee){

        String empId = (String) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改成功");

    }

}
